package com.menu.system.service.impl;

import com.menu.common.core.domain.TreeSelect;
import com.menu.common.core.domain.entity.MenuProtype;
import com.menu.common.utils.DateUtils;
import com.menu.system.mapper.MenuInformtypeMapper;
import com.menu.system.service.IMenuInformtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单权限Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-05
 */
@Service
public class MenuInformtypeServiceImpl implements IMenuInformtypeService {
    @Autowired
    private MenuInformtypeMapper menuInformtypeMapper;

    /**
     * 查询菜单权限
     *
     * @param productId 菜单权限主键
     * @return 菜单权限
     */
    @Override
    public MenuProtype selectMenuInformtypeByProductId(Long productId) {
        return menuInformtypeMapper.selectMenuInformtypeByProductId(productId);
    }

    /**
     * 查询菜单权限列表
     *
     * @param menuProtype 菜单权限
     * @return 菜单权限
     */
    @Override
    public List<MenuProtype> selectMenuInformtypeList(MenuProtype menuProtype) {
        return menuInformtypeMapper.selectMenuInformtypeList(menuProtype);
    }

    /**
     * 新增菜单权限
     *
     * @param menuProtype 菜单权限
     * @return 结果
     */
    @Override
    public int insertMenuInformtype(MenuProtype menuProtype) {
        menuProtype.setCreateTime(DateUtils.getNowDate());
        return menuInformtypeMapper.insertMenuInformtype(menuProtype);
    }

    /**
     * 修改菜单权限
     *
     * @param menuProtype 菜单权限
     * @return 结果
     */
    @Override
    public int updateMenuInformtype(MenuProtype menuProtype) {
        menuProtype.setUpdateTime(DateUtils.getNowDate());
        return menuInformtypeMapper.updateMenuInformtype(menuProtype);
    }

    /**
     * 批量删除菜单权限
     *
     * @param productIds 需要删除的菜单权限主键
     * @return 结果
     */
    @Override
    public int deleteMenuInformtypeByProductIds(Long[] productIds) {
        return menuInformtypeMapper.deleteMenuInformtypeByProductIds(productIds);
    }

    /**
     * 删除菜单权限信息
     *
     * @param productId 菜单权限主键
     * @return 结果
     */
    @Override
    public int deleteMenuInformtypeByProductId(Long productId) {
        return menuInformtypeMapper.deleteMenuInformtypeByProductId(productId);
    }

    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<MenuProtype> menus) {
        List<MenuProtype> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<MenuProtype> buildMenuTree(List<MenuProtype> menus) {
        List<MenuProtype> returnList = new ArrayList<MenuProtype>();
        List<Long> tempList = menus.stream().map(MenuProtype::getProductId).collect(Collectors.toList());
        for (Iterator<MenuProtype> iterator = menus.iterator(); iterator.hasNext(); ) {
            MenuProtype menu = (MenuProtype) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId())) {
                recursionFns(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }

    private void recursionFns(List<MenuProtype> list, MenuProtype t) {
        // 得到子节点列表
        List<MenuProtype> childList = getChildList(list, t);
        t.setChildren(childList);
        for (MenuProtype tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFns(list, tChild);
            }
        }
    }

    private List<MenuProtype> getChildList(List<MenuProtype> list, MenuProtype t) {
        List<MenuProtype> tlist = new ArrayList<MenuProtype>();
        Iterator<MenuProtype> it = list.iterator();
        while (it.hasNext()) {
            MenuProtype n = (MenuProtype) it.next();
            if (n.getParentId().longValue() == t.getProductId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<MenuProtype> list, MenuProtype t) {
        return getChildList(list, t).size() > 0;
    }
}
